<?php
include_once ("inc/conn.php");
$query = "SELECT  SUBSTRING(ConFileStr,2,LEN(ConFileStr))ConFileStr  FROM  a_custnew WHERE DJTYPE='$DJTYPE' AND sm='H' ";
$cursor = msquery ( $msconn, $query );
$ROWS = mssql_fetch_array ( $cursor );
include_once ($ROWS [ConFileStr]);
$SQL = "SELECT ";
$query = "SELECT FIELD,TYPE,PLACE,CODE_LIST,K_TYPE,RE_TBNAME,RE_FDNAME,RE_IDFNAME,A_RETURN FROM  a_custdj WHERE  DJTYPE='$DJTYPE' and IsEnabled=1  AND PLACE='H' ";
$cursor = msquery ( $msconn, $query );
while ( $ROW = mssql_fetch_array ( $cursor ) ) {
	if ($ROW [CODE_LIST] != "" && $ROW [CODE_LIST] != " " && $ROW [K_TYPE] != "TEXT") {
		$SQL = $SQL . " dbo.SelectGetIdByName('$ROW[CODE_LIST]',H." . $ROW [FIELD] . ") as " . $ROW [PLACE] . "_$ROW[FIELD],";
	} else if ($ROW [A_RETURN] == "1" && $ROW [RE_TBNAME] == "CUST" && $ROW [RE_FDNAME] == "NAME" && $ROW [RE_IDFNAME] == "CUS_NO") {
		$SQL = $SQL . " dbo.cust_GetIdByName(H.$ROW[FIELD]) as " . $ROW [PLACE] . "_$ROW[FIELD],";
	} else if ($ROW [A_RETURN] == "1" && $ROW [RE_TBNAME] == "SALM" && $ROW [RE_FDNAME] == "NAME" && $ROW [RE_IDFNAME] == "SAL_NO") {
		$SQL = $SQL . " dbo.salm_GetIdByName(H.$ROW[FIELD]) as " . $ROW [PLACE] . "_$ROW[FIELD],";
	} else if ($ROW [A_RETURN] == "1" && $ROW [RE_TBNAME] == "DEPT" && $ROW [RE_FDNAME] == "NAME" && $ROW [RE_IDFNAME] == "DEP") {
		$SQL = $SQL . " dbo.dept_GetIdByName(H.$ROW[FIELD]) as " . $ROW [PLACE] . "_$ROW[FIELD],";
	} else if ($ROW [A_RETURN] == "1" && $ROW [RE_TBNAME] == "MY_WH" && $ROW [RE_FDNAME] == "NAME" && $ROW [RE_IDFNAME] == "WH") {
		$SQL = $SQL . " dbo.wh_GetIdByName(H.$ROW[FIELD]) as " . $ROW [PLACE] . "_$ROW[FIELD],";
	} else {
		$SQL = $SQL . " H." . $ROW [FIELD] . " as " . $ROW [PLACE] . "_$ROW[FIELD],";
	}
}
$query = "SELECT FIELD,TYPE,PLACE,CODE_LIST,K_TYPE,K_TYPE,RE_TBNAME,RE_FDNAME,RE_IDFNAME,A_RETURN FROM  a_custdj WHERE  DJTYPE='$DJTYPE' and IsEnabled=1  AND PLACE='B' ";
$cursor = msquery ( $msconn, $query );
while ( $ROW = mssql_fetch_array ( $cursor ) ) {
	if ($ROW [CODE_LIST] != "" && $ROW [CODE_LIST] != " " && $ROW [K_TYPE] != "TEXT") {
		$SQL = $SQL . " dbo.SelectGetIdByName('$ROW[CODE_LIST]',B." . $ROW [FIELD] . ") as " . $ROW [PLACE] . "_$ROW[FIELD],";
	} else if ($ROW [A_RETURN] == "1" && $ROW [RE_TBNAME] == "CUST" && $ROW [RE_FDNAME] == "NAME" && $ROW [RE_IDFNAME] == "CUS_NO") {
		$SQL = $SQL . " dbo.cust_GetIdByName(B.$ROW[FIELD]) as " . $ROW [PLACE] . "_$ROW[FIELD],";
	} else if ($ROW [A_RETURN] == "1" && $ROW [RE_TBNAME] == "SALM" && $ROW [RE_FDNAME] == "NAME" && $ROW [RE_IDFNAME] == "SAL_NO") {
		$SQL = $SQL . " dbo.salm_GetIdByName(B.$ROW[FIELD]) as " . $ROW [PLACE] . "_$ROW[FIELD],";
	} else if ($ROW [A_RETURN] == "1" && $ROW [RE_TBNAME] == "DEPT" && $ROW [RE_FDNAME] == "NAME" && $ROW [RE_IDFNAME] == "DEP") {
		$SQL = $SQL . " dbo.dept_GetIdByName(B.$ROW[FIELD]) as " . $ROW [PLACE] . "_$ROW[FIELD],";
	} else if ($ROW [A_RETURN] == "1" && $ROW [RE_TBNAME] == "MY_WH" && $ROW [RE_FDNAME] == "NAME" && $ROW [RE_IDFNAME] == "WH") {
		$SQL = $SQL . " dbo.wh_GetIdByName(B.$ROW[FIELD]) as " . $ROW [PLACE] . "_$ROW[FIELD],";
	} else {
		$SQL = $SQL . " B." . $ROW [FIELD] . " as " . $ROW [PLACE] . "_$ROW[FIELD],";
	}
}

$SQL = $SQL . "100" . " FROM " . $Table . " H LEFT JOIN  " . $Table_Sub . " B ";
$SQL = $SQL . "ON H." . $Table_KEY . " = B." . $Table_Sub_Related;
$SQL = $SQL . " WHERE H." . $Table_KEY . "='JQKEY'";
echo $SQL;
?>
